package com.thrc.borrow.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.thrc.borrow.entity.News;
import com.thrc.borrow.service.NewsService;
import com.thrc.borrow.utils.Container;
import com.thrc.borrow.utils.DateUtil;
import com.thrc.borrow.utils.ResponseUtil;
import com.thrc.borrow.utils.ReturnCode;
import com.thrc.borrow.vo.NewsVo;

@Controller
@RequestMapping("/news")
public class NewsController {
	
	@Autowired
	private NewsService newsService;
	
	@RequestMapping("/list/page")
	public String goNewsJsp() {
		return "sys/newsList";
	}
	
	@RequestMapping(value = "/getTypeJson")
	public void getTypeJson(HttpServletResponse response) {
		JSONArray retJson = new JSONArray();

/*		JSONObject noticeJson = new JSONObject();
		noticeJson.put(ReturnCode.RETURN_CODE, Container.NEWS_TYPE.NOTICE);
		noticeJson.put(ReturnCode.RETURN_DATA, "公司公告");*/

		JSONObject newsJson = new JSONObject();
		newsJson.put(ReturnCode.RETURN_CODE, Container.NEWS_TYPE.NEWS);
		newsJson.put(ReturnCode.RETURN_DATA, "行业动态");
		
/*		JSONObject problem = new JSONObject();
		problem.put(ReturnCode.RETURN_CODE, Container.NEWS_TYPE.PROBLEM);
		problem.put(ReturnCode.RETURN_DATA, "常见问题");*/

		/*retJson.add(noticeJson);*/
		retJson.add(newsJson);
		/*retJson.add(problem);*/

		ResponseUtil.print(response, retJson);

	}
	
	@RequestMapping(value = "/getStatusJson")
	public void getStatusJson(HttpServletResponse response) {
		JSONArray retJson = new JSONArray();
		
		JSONObject noticeJson = new JSONObject();
		noticeJson.put(ReturnCode.RETURN_CODE, Container.NEWS_STATUS.OPEN);
		noticeJson.put(ReturnCode.RETURN_DATA, "开启");
		
		JSONObject newsJson = new JSONObject();
		newsJson.put(ReturnCode.RETURN_CODE, Container.NEWS_STATUS.CLOSE);
		newsJson.put(ReturnCode.RETURN_DATA, "关闭");
		
		retJson.add(noticeJson);
		retJson.add(newsJson);
		
		ResponseUtil.print(response, retJson);
		
	}
	
	@RequestMapping("/selectNewsListByPage")
	public void selectByPage(HttpServletResponse response, News t) {
		JSONObject retJson = new JSONObject();
		try {
			int totleCount = newsService.selectCount(t);
			retJson.put("total", totleCount);
			if (totleCount == 0) {
				retJson.put("data", null);
				ResponseUtil.print(response, retJson);
				return;
			}
			List<NewsVo> newsList = newsService.selectByPage(t);
			retJson.put("data", newsList);
			ResponseUtil.print(response, retJson);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}
	
	@RequestMapping("/selectAllNews")
	public void selectAll(HttpServletResponse response, News t) {
		try {
			if(StringUtils.isEmpty(t.getStatus()+"")){
				t.setStatus(Container.NEWS_STATUS.OPEN);
			}
			List<NewsVo> newsList = newsService.selectAll(t);
			ResponseUtil.print(response, newsList);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}
	
	@RequestMapping("/getFrontNews")
	public void getFrontNews(HttpServletResponse response, News t) {
		try {
			NewsVo news = newsService.selectById(t);
			ResponseUtil.print(response, news);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}
	
	@RequestMapping("/insertNews")
	public void insert(HttpServletResponse response, News t) {
		try {
			t.setCreateTime(DateUtil.getDateString(new Date(), "yyyy-MM-dd HH:mm:ss"));
			newsService.insert(t);
			ResponseUtil.print(response, ReturnCode.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}
	
	@RequestMapping("/updateNews")
	public void update(HttpServletResponse response, News t) {
		try {
			newsService.update(t);
			ResponseUtil.print(response, ReturnCode.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}
	
	@RequestMapping("/deleteNews")
	public void delete(HttpServletResponse response, News t) {
		try {
			newsService.delete(t);
			ResponseUtil.print(response, ReturnCode.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}
	
	@RequestMapping("/getNewsById")
	public void selectById(HttpServletResponse response, News t) {
		ResponseUtil.print(response, newsService.selectById(t));
	}
}
